import { Color, Mark } from "./enums";
import { Card,  Joker } from "./types";

interface dealCardResult{
    player1:Deck,
    player2:Deck,
    player3:Deck,
    left:Deck,

}

export class Deck{ 
    private cards:Card [] = [];    
    constructor(cards?:Card[]){
        if(cards){
            this.cards = cards
        }else{
            this.init()
        }
    }
     print(){
        // this.cards.forEach((i) => {
        //     console.log(i.getString());
        //   });
          let result = "\n";
          this.cards.forEach((card, i) => {
              result += card.getString() + "\t";
              if ((i + 1) % 6 === 0) {
                  result += "\n";
              }
          })
          console.log(result);
    }

    private init(){
       console.log("初始化");
       
      //获取到枚举对象的值数组;
      const marks = Object.values(Mark) 
      const colors = Object.values(Color)
      for (const m of marks) {
        for (const c of colors) {
          // 加入普通卡牌
          this.cards.push({
                color:c,
                mark:m,
                getString() { 
                  return this.color + this.mark;
                },
            } as Card)//告诉ts插入的对象为Card
            // 或
          //   let card:NormalCard = {
          //     color:c,
          //     mark:m,
          //     getString() { 
          //       return this.color + this.mark;
          //     },
          // }
          // deck.push(card)
          // 或 
        //   deck.push(<Card>{
        //     color:c,
        //     mark:m,
        //     getString() { 
        //       return this.color + this.mark;
        //     },
        // }) 但不推荐使用，因为react中为一个组件会发生冲突
         }
      }
       // 加入大小王
       let joker: Joker = {
        type:"small",
        getString() {
          return "jo"
        },
      }
        this.cards.push(joker);

      joker={
        type : "big",
        getString() {
          return "JO"
        },
      }
      this.cards.push(joker);
    }
    shuffle(){
        for (let i = 0 ; i < this.cards.length ; i++) {
            const index = this.getRandom(0,this.cards.length);
            const temp = this.cards[i];
            this.cards[i] = this.cards[index];
            this.cards[index] = temp;

        }
    }
    
    dealCard():dealCardResult {
        let player1:Deck,player2:Deck,player3:Deck,left:Deck;
        player1 = this.takeCards(17);
        player2 = this.takeCards(17);
        player3 = this.takeCards(17);
        left = new Deck(this.cards);
        return {
            player1,
            player2,
            player3,
            left,
        }
    }
    private takeCards(n:number):Deck{
        const cards : Card[] = [];
        for (let index = 0; index < n; index++) {
            cards.push(this.cards.shift() as Card)
            
        }
        return new Deck(cards)
    }
    /**
     * 无法取到最大值
     * @param min 
     * @param max 
     */
    private getRandom(min: number, max: number) {
        const dec = max - min;
        return Math.floor(Math.random() * dec + min);
    }

}